#include "stdafx.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "GenSort.h"

const int SPEED_STOP = 0;
const int DIRECTION_NORTH = 0;

#pragma data_seg(".MYSEG")
	int nProcessCount = 0;
	int nThreadCount  = 0;
	int nValue = 0;
	int nValue2 = 0;
	int curSpeed = SPEED_STOP;
	int curDirection = DIRECTION_NORTH;
	bool bChildActive = false;
#pragma data_seg()

#pragma comment(linker, "/SECTION:.MYSEG,RWS")

BOOL APIENTRY DLLMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
	TCHAR sString[255];
	sprintf(sString, "Handle to GenSort instance is \'%ld\'\n", hModule);
	OutputDebugString(sString);

	switch(dwReason)
	{
		case DLL_PROCESS_ATTACH:	// Init code here.
									//................
									nProcessCount++;
									return(TRUE);

		case DLL_PROCESS_DETACH:	// Cleanup code here.
									//...................
									nProcessCount--;
									break;                      

		case DLL_THREAD_ATTACH:		// Thread specific init code here.
									// This is so the DLL can "Thread Protect" itself.
									//................................................
									nThreadCount++;
									break;

		case DLL_THREAD_DETACH:		// Thread specific cleanup code here.
									//...................................
									nThreadCount--;
									break;
	}

	sprintf(sString, "nProcessCount = %d, nThreadCount = %d\n", nProcessCount, nThreadCount);
	OutputDebugString(sString);

	return TRUE;
}

GENSORT_API int GetProcessCount() { return nProcessCount; }
GENSORT_API int GetThreadCount()  { return nThreadCount;  }

GENSORT_API void *DataValueAddress()
{
	return (void*) &nValue;
}

GENSORT_API int DataValueGet()
{
	return nValue;
}

GENSORT_API void DataValueSet(int Value)
{
	nValue = Value;
}

GENSORT_API int DataValue2Get()
{
	return nValue2;
}

GENSORT_API void DataValue2Set(int Value)
{
	nValue2 = Value;
}

GENSORT_API int CurSpeedGet()
{
	return curSpeed;
}

GENSORT_API void CurSpeedSet(int Value)
{
	curSpeed = Value;
}

GENSORT_API int CurDirectionGet()
{
	return curDirection;
}

GENSORT_API void CurDirectionSet(int Value)
{
	curDirection = Value;
}

GENSORT_API void ChildDown()
{
	bChildActive = false;
}

GENSORT_API bool ChildActive()
{
	return bChildActive;
}

GENSORT_API void SetChildStatus(bool bValue)
{
	bChildActive = bValue;
}
